home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / fp / ifp_unix.lzh / ifp / interp / F_string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-23  |  3.7 KB  |  141 lines

  1.  
  2. /****** F_string.c ****************************************************/
  3. /**                                                                  **/
  4. /**                    University of Illinois                        **/
  5. /**                                                                  **/
  6. /**                Department of Computer Science                    **/
  7. /**                                                                  **/
  8. /**   Tool: IFP                         Version: 0.5                 **/
  9. /**                                                                  **/
  10. /**   Author:  Arch D. Robison          Date:   May 1, 1985          **/
  11. /**                                                                  **/
  12. /**   Revised by: Arch D. Robison       Date:  July 5, 1985          **/
  13. /**                                                                  **/
  14. /**   Principal Investigators: Prof. R. H. Campbell                  **/
  15. /**                            Prof. W. J. Kubitz                    **/
  16. /**                                                                  **/
  17. /**                                                                  **/
  18. /**------------------------------------------------------------------**/
  19. /**   (C) Copyright 1987  University of Illinois Board of Trustees   **/
  20. /**                       All Rights Reserved.                       **/
  21. /**********************************************************************/
  22.  
  23. #include <stdio.h>
  24. #include "struct.h"
  25. #include "string.h"
  26. #include "node.h"
  27.  
  28. /*
  29.  * F_Patom
  30.  *
  31.  * Convert an atom to it's string representation.
  32.  */
  33. private F_Patom (InOut)
  34.    register ObjectPtr InOut;
  35.    {
  36.       CharPtr U;
  37.       char Buf[255];
  38.       StrPtr S;
  39.       register char *T;
  40.  
  41.       T = Buf;
  42.       switch (InOut->Tag) {
  43.      case INT:
  44.         (void) sprintf (T,"%d",InOut->Int);
  45.         break;
  46.      case FLOAT:
  47.         (void) sprintf (T,"%g",InOut->Float);
  48.         break;
  49.      case BOOLEAN:
  50.         (void) sprintf (T,InOut->Bool ? "t":"f");
  51.         break;
  52.      case STRING:
  53.         return;
  54.      default:
  55.         FunError ("not atomic",InOut);
  56.         return;
  57.       }
  58.       S = NULL;
  59.       CPInit (&U,&S);
  60.       do CPAppend (&U,*T); while (*T++);
  61.       RepTag (InOut,STRING);
  62.       InOut->String = S;
  63.    }
  64.  
  65.  
  66. /*
  67.  * F_Explode
  68.  *
  69.  * Convert a string to a list of characters
  70.  */
  71. private F_Explode (InOut)
  72.    ObjectPtr InOut;
  73.    {
  74.       ListPtr Result = NULL;
  75.       MetaPtr A = &Result;
  76.       CharPtr U;
  77.       char C[2];
  78.  
  79.       if (InOut->Tag != STRING)
  80.      FunError ("not a string",InOut);
  81.       else {
  82.      CPInit (&U,&InOut->String);
  83.      while (CPRead (&U,C,2)) {
  84.         NewList (A,1L);
  85.         if (SysError) {DelLPtr (Result); return;}
  86.         (*A)->Val.Tag = STRING;
  87.         (*A)->Val.String = CopySPtr (CharString [C[0] & 0x7F]);
  88.         A = &(*A)->Next;
  89.      }
  90.      RepTag (InOut,LIST);
  91.      InOut->List = Result;
  92.       }
  93.    }
  94.  
  95.  
  96. /*
  97.  * F_Implode
  98.  *
  99.  * Catenate a list of strings into a single string.
  100.  */
  101. private F_Implode (InOut)
  102.    ObjectPtr InOut;
  103.    {
  104.       CharPtr U,V;
  105.       char C[2];
  106.       ListPtr P;
  107.       StrPtr S;
  108.  
  109.       if (InOut->Tag != LIST)
  110.      FunError ("not a sequence",InOut);
  111.       else {
  112.      S = NULL;
  113.      CPInit (&U,&S);
  114.      for (P = InOut->List; P != NULL; P=P->Next) {
  115.         if (P->Val.Tag != STRING) {
  116.            FunError ("non-string in sequence",InOut);
  117.            CPAppend (&U,'\0');
  118.            DelSPtr (S);
  119.            return;
  120.         } else {
  121.            CPInit (&V,&P->Val.String);
  122.            while (CPRead (&V,C,2)) CPAppend (&U,C[0]);
  123.         }
  124.      }
  125.      CPAppend (&U,'\0');
  126.      RepTag (InOut,STRING);
  127.      InOut->String = S;
  128.       }
  129.    }
  130.  
  131.  
  132. void D_string ()
  133.    {   
  134.       (void) PrimDef (F_Explode,"explode",SysNode);
  135.       (void) PrimDef (F_Implode,"implode",SysNode);
  136.       (void) PrimDef (F_Patom,"patom",SysNode);
  137.    }
  138.  
  139. /************************** end of F_string **************************/
  140.  
  141.